package com.ls.pattern.singleton;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 注册式单例
 */
public class RegisterSingleton {

    private static final Map<String, Object> IOC = new ConcurrentHashMap<>();

    private RegisterSingleton() {
    }

    public static Object getBean(String className) {
        if (!IOC.containsKey(className)) {
            synchronized (RegisterSingleton.class) {
                if (!IOC.containsKey(className)) {
                    try {
                        Object obj = Class.forName(className).newInstance();
                        IOC.put(className, obj);
                        return obj;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

        }
        return IOC.get(className);
    }

}
